﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;

using Carbon.Web.Forms.jQueryForms.DataEditors.UploadEditor;

namespace Carbon.Web.Forms.jQueryForms
{
    public partial class FileUploadEditorLayout : Carbon.Web.Forms.Controls.FileUpload.FileUploadEditorLayout, IScriptControl
    {
		#region Classes (1)
		private class FileUploadEditorLayoutScriptDescriptor : ScriptDescriptor
        {
			#region Fields (1)
			private FileUploadEditorLayout m_Parent = default(FileUploadEditorLayout);
			#endregion

			#region Properties (1)
			public FileUploadEditorLayout Parent
            {
                get
                {
                    return m_Parent;
                }
            }
			#endregion

			#region Constructors (1)
			public FileUploadEditorLayoutScriptDescriptor(FileUploadEditorLayout parent)
                : base()
            {
                if (parent == null) throw new ArgumentNullException("parent");

                this.m_Parent = parent;
            }
			#endregion

			#region Methods (1)

			protected override string GetScript()
            {
                return string.Format("Carbon.Web.Forms.jQueryForms.FileUploadEditor(\"{0}\");", Parent.ClientID);
            }

			#endregion
        }
		#endregion

		#region Properties (1)
		public override string PopupUrl
        {
            get
            {
                String path = PathUtility.Join(Configuration.Instance.PagesPath, "UploadEditorPopup.aspx", '/');

                if (path.StartsWith("~"))
                    path = path.TrimStart('~');

                String extensions = string.Join("; ", this.FileUploadEditor.AllowedExtensions);

                path = path + "?MaxFiles=" + NumberOfAdditionalFilesThatCanBeUploaded + "&AllowedExtensions=" + HttpUtility.UrlEncode(extensions);

                return path;
            }
        }
		#endregion

		#region ASP Processing

		protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

		protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            ScriptManager sm = ScriptManager.GetCurrent(this.Page);
            if (sm == null) throw new NullReferenceException("sm");

            sm.RegisterScriptControl(this);
            sm.RegisterScriptDescriptors(this);
        }

		#endregion

		#region Methods (2)

		public IEnumerable<ScriptDescriptor> GetScriptDescriptors()
        {
            return new ScriptDescriptor[] { new FileUploadEditorLayoutScriptDescriptor(this) };
        }

		public IEnumerable<ScriptReference> GetScriptReferences()
        {
            return EmptyArray<ScriptReference>.Instance;
        }

		#endregion

		#region Event Handlers (2)

		protected void Popup_ReturnedData(object sender, PopupViewModelChangedEventArgs<UploadEditorPopupViewModel> args)
        {
            UploadEditorPopupViewModel viewModel = args.ViewModel;

            foreach (FileReference reference in viewModel.References)
            {
                String sId = ConvertInvariant.ToString(reference.Id);
                FileReference newReference = new FileReference(InternalUtility.CompactDeserialize<Object>(sId), reference.Key, reference.Name);
                References.Add(newReference);
            }

            this.RaiseBubbleEvent(this, new UpdateModelEventArgs());
        }

		protected void UpdatingControl_FileName(object sender, BindEventArgs args)
        {
            String name = Convert.ToString(args.ModelValue);

            if (string.IsNullOrEmpty(name))
                args.ReturnValue = Resources.GetString("Empty");
        }

		#endregion

        public override void OnUpdateControl(object sender, UpdateControlEventArgs args)
        {
            DataBind();
        }

        public override void OnUpdateModel(object sender, UpdateModelEventArgs args)
        {
        }
    }
}